package com.fh.controller.dog.admin;

import com.fh.controller.base.BaseController;
import com.fh.entity.Page;
import com.fh.entity.system.User;
import com.fh.service.admin.dictionary.impl.DictionaryService;
import com.fh.service.system.fhlog.FHlogManager;
import com.fh.util.*;
import com.fh.util.layui.DataUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * 类名称：DictionaryController
 *
 * @version 1.0
 * @author：Lr 更新时间：2017年12月21日
 */
@Controller
@RequestMapping(value = "/dictionary")
public class DictionaryController extends BaseController {

    /**
     * 菜单地址(权限用)
     */
    private String menuUrl = "dictionary/list.do";
    @Resource(name = "dictionaryService")
    private DictionaryService dictionaryService;
    @Resource(name = "fhlogService")
    private FHlogManager FHLOG;

    /**
     * 显示字典列表
     *
     * @param page
     * @return String
     * @throws Exception
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public String list(Page page) throws Exception {
        PageData pd = this.getPageData();
        String data = "";
        try {
            //关键词检索条件
            String keywords = pd.getString("keywords");
            if (null != keywords && !"".equals(keywords)) {
                pd.put("keywords", keywords.trim());
            }
            page.setPd(pd);
            DataUtil.setPage(page, pd);
            List<PageData> list = dictionaryService.list(page);
            data = DataUtil.getJsonData(page, list);
        } catch (Exception e) {
            logger.error(e.toString(), e);
        } finally {
            logAfter(logger);
        }
        return data;
    }

    /**
     * 保存字典
     *
     * @param
     * @return PageData
     * @throws Exception
     */
    @RequestMapping(value = "/save")
    @ResponseBody
    public PageData save() throws Exception {
        //校验权限
        String type = "add";
        if (!Jurisdiction.buttonJurisdiction(menuUrl, type)) {
            return null;
        }
        logBefore(logger, Jurisdiction.getUsername() + "新增dictionary");
        PageData result = new PageData();
        PageData pd = this.getPageData();
        User user = SessionUtil.getSessionUser();
        try {
            pd.put("ID", UuidUtil.get32UUID());
            pd.put("PARENT_ID", 0);
            pd.put("DIC_TYPE", 1);
            pd.put("IS_DELETED", 0);
            pd.put("FOUNDER_ID", user.getUSER_ID());
            pd.put("GMT_CREATE", DateUtil.getTime());
            pd.put("MODIFIER_ID", user.getUSER_ID());
            pd.put("GMT_MODIFIED", DateUtil.getTime());
            //执行保存
            dictionaryService.save(pd);
            result.put("data", "");
            result.put("returnCode", "00");
            result.put("returnMsg", "新增字典成功");
            FHLOG.save(Jurisdiction.getUsername(), "新增字典：" + pd.getString("USERNAME"));
            return result;
        } catch (Exception e) {
            logger.error(e.toString(), e);
            result.put("data", "");
            result.put("returnCode", "01");
            result.put("returnMsg", "新增字典失败");
            FHLOG.save(Jurisdiction.getUsername(), "新增字典失败：" + pd.getString("USERNAME"));
            return result;
        }
    }

    /**
     * 查看字典
     *
     * @param
     * @return PageData
     * @throws Exception
     */
    @RequestMapping(value = "/view")
    @ResponseBody
    public PageData view() throws Exception {
        //校验权限
        String type = "cha";
        if (!Jurisdiction.buttonJurisdiction(menuUrl, type)) {
            return null;
        }
        PageData result = new PageData();
        PageData pd = this.getPageData();
        try {
            //根据ID读取
            pd = dictionaryService.findById(pd);
            result.put("data", pd);
            result.put("returnCode", "00");
            result.put("returnMsg", "查看字典成功");
            return result;
        } catch (Exception e) {
            logger.error(e.toString(), e);
            result.put("data", "");
            result.put("returnCode", "01");
            result.put("returnMsg", "查看字典失败");
            return result;
        }
    }

    /**
     * 修改字典
     *
     * @param
     * @return PageData
     * @throws Exception
     */
    @RequestMapping(value = "/edit")
    @ResponseBody
    public PageData edit() throws Exception {
        logBefore(logger, Jurisdiction.getUsername() + "修改dictionary");
        PageData result = new PageData();
        PageData pd = this.getPageData();
        User user = SessionUtil.getSessionUser();
        try {
            pd.put("MODIFIER_ID", user.getUSER_ID());
            pd.put("GMT_MODIFIED", DateUtil.getTime());
            //执行修改
            dictionaryService.edit(pd);
            result.put("data", "");
            result.put("returnCode", "00");
            result.put("returnMsg", "修改字典成功");
            FHLOG.save(Jurisdiction.getUsername(), "修改字典：" + pd.getString("USERNAME"));
            return result;
        } catch (Exception e) {
            logger.error(e.toString(), e);
            result.put("data", "");
            result.put("returnCode", "01");
            result.put("returnMsg", "修改字典失败");
            FHLOG.save(Jurisdiction.getUsername(), "修改字典失败：" + pd);
            return result;
        }
    }

    /**
     * 删除字典
     *
     * @param
     * @return PageData
     * @throws Exception
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public PageData delete() throws Exception {
        //校验权限
        String type = "del";
        if (!Jurisdiction.buttonJurisdiction(menuUrl, type)) {
            return null;
        }
        logBefore(logger, Jurisdiction.getUsername() + "删除dictionary");
        PageData result = new PageData();
        PageData pd = this.getPageData();
        try {
            dictionaryService.delete(pd);
            result.put("data", "");
            result.put("returnCode", "00");
            result.put("returnMsg", "删除字典成功");
            FHLOG.save(Jurisdiction.getUsername(), "删除字典：" + pd);
            return result;
        } catch (Exception e) {
            logger.error(e.toString(), e);
            result.put("data", "");
            result.put("returnCode", "01");
            result.put("returnMsg", "删除字典失败");
            FHLOG.save(Jurisdiction.getUsername(), "删除字典失败：" + pd);
            return result;
        }
    }


    /**
     * 查看字典（全部）
     *
     * @param
     * @return PageData
     * @throws Exception
     */
    @RequestMapping(value = "/listAll")
    @ResponseBody
    public PageData listAll() throws Exception {
        //校验权限
        String type = "cha";
        if (!Jurisdiction.buttonJurisdiction(menuUrl, type)) {
            return null;
        }
        PageData result = new PageData();
        PageData pd = this.getPageData();
        try {
            //根据DIC_GROUP获取字典列表
            List<PageData> list = dictionaryService.listAll(pd);
            result.put("data", list);
            result.put("returnCode", "00");
            result.put("returnMsg", "获取字典列表成功");
            return result;
        } catch (Exception e) {
            logger.error(e.toString(), e);
            result.put("data", "");
            result.put("returnCode", "01");
            result.put("returnMsg", "获取字典列表失败");
            return result;
        }
    }

}
